﻿Public Class DanToc
    Private status As Boolean = False
    Private data As DataTable
    Private dlieu As String
    Private vt As Integer = 0
    Private Sub bt_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_them.Click
        If tb_tendt.Text = "" Then
            MessageBox.Show("Dữ Liệu Nhập Vào Bị Sai", "Thông Báo", MessageBoxButtons.OK)
        Else
            Dim dt As DataRow = data.NewRow()
            dt("TenDT") = tb_tendt.Text
            data.Rows.Add(dt)
            Dim item As New ListViewItem
            item.Text = "STT"
            item.SubItems.Add(dt("TenDT"))
            lv_dantoc.Items.Add(item)
            CapNhatSTT()
            Refesh_()
        End If
    End Sub

    Private Sub bt_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_xoa.Click
        If status = True Then
            For Each dr As DataRow In data.Rows
                If dr("TenDT") = dlieu Then
                    dr.Delete() 'xoa du lieu tai dong so sánh
                    lv_dantoc.Items.RemoveAt(vt)
                    Exit For
                End If
            Next
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cập Nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_cn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_cn.Click
        If status = True Then
            For Each dr As DataRow In data.Rows
                If dr("TenDT") = dlieu Then
                    dr("TenDT") = tb_tendt.Text
                    lv_dantoc.Items(vt).SubItems(1).Text = tb_tendt.Text ' cap nhat lai trên listview
                End If
            Next
            Refesh_()
        Else
            MessageBox.Show("Chưa Chọn Đối Tượng Cập Nhật", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_lamlai.Click
        Refesh_()
    End Sub

    Private Sub lv_dantoc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_dantoc.SelectedIndexChanged
        Try
            vt = lv_dantoc.SelectedItems(0).Index()
            tb_tendt.Text = lv_dantoc.SelectedItems(0).SubItems(1).Text
            dlieu = lv_dantoc.SelectedItems(0).SubItems(1).Text
            status = True
        Catch ex As Exception
        End Try
    End Sub

    Private Sub DanToc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        data = New DataTable
        Dim xl As New XLDanToc
        data = xl.BUS_LayDSDT
        showLV()
    End Sub

    Private Sub DanToc_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            Dim xl As New XLDanToc
            If xl.BUS_SaveDT(data) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub
    Private Sub showLV()
        If data.Rows.Count < 0 Then Return
        lv_dantoc.Items.Clear()
        lv_dantoc.View = View.Details
        lv_dantoc.GridLines = True
        lv_dantoc.FullRowSelect = True
        For Each dt As DataRow In data.Rows
            Dim item As New ListViewItem
            item.Text = "STT"
            item.SubItems.Add(dt("TenDT"))
            lv_dantoc.Items.Add(item)
            CapNhatSTT()
        Next
    End Sub
    Private Sub CapNhatSTT()
        For i As Integer = 1 To lv_dantoc.Items.Count
            lv_dantoc.Items(i - 1).Text = i.ToString
        Next
    End Sub
    Private Sub Refesh_()
        tb_tendt.Text = String.Empty
        dlieu = String.Empty
        status = False
    End Sub
End Class